# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name： selenium_spider
Description :
Author : 'li'
date： 2021/7/8
-------------------------------------------------
Change Activity:
2021/7/8:
-------------------------------------------------
"""
import time

from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By

from .. import logger


class SeleniumUtility:
    def __init__(self, driver=None, driver_path=None):
        if driver is None:
            driver = self._load_driver(driver_path)
        self.driver = driver

    def open_page(self, url, is_maximize_window=True):
        """

        Args:
            is_maximize_window:
            url: web url

        Returns:

        """
        self.driver.get(url)
        if is_maximize_window:
            self.driver.maximize_window()
        self.driver.implicitly_wait(10)

    @staticmethod
    def _load_driver(driver_path):
        driver = Firefox(executable_path=driver_path)
        logger.debug(rf'load driver from {driver_path}.')
        return driver

    @staticmethod
    def wait_element_is_displayed(element, wait_time=10):
        """

        """
        loop_time = int(wait_time / 0.5)
        for i in range(loop_time):
            if element.is_displayed():
                return True
            time.sleep(0.5)
        return False

    def find_element_is_displayed_by_class_name(self, css_name, wait_time=3):
        """

        """
        loop_time = int(wait_time / 0.5)
        for i in range(loop_time):
            elements = self.driver.find_elements_by_class_name(css_name)
            if len(elements) > 0:
                return elements
            time.sleep(0.5)
        return None

    def click_element_by_id(self, tag_id):
        login_warn_confirm_btn = self.driver.find_element(by=By.ID, value=tag_id)
        SeleniumUtility.wait_element_is_displayed(login_warn_confirm_btn, 3)
        login_warn_confirm_btn.click()

    def click_element(self, element):
        """

        Args:
            element:

        Returns:

        """
        SeleniumUtility.wait_element_is_displayed(element, 3)
        element.click()
        self.driver.implicitly_wait(10)

    def find_elements(self, by, value):
        """

        Args:
            by:
            value:

        Returns:

        """
        return self.driver.find_elements(by, value)

    def find_element_by_id(self, id):
        """

        Args:
            id:

        Returns:

        """
        self.driver.implicitly_wait(10)
        element = self.driver.find_element(by=By.ID, value=id)
        return element

    def find_elements_by_name(self, name):
        """

        Args:
            name:

        Returns:

        """
        self.driver.implicitly_wait(10)
        element = self.driver.find_elements(by=By.NAME, value=name)
        return element

    def find_elements_by_tag_name(self, name):
        """

        Args:
            name:

        Returns:

        """
        self.driver.implicitly_wait(10)
        elements = self.driver.find_elements(by=By.TAG_NAME, value=name)
        return elements

    def input_element_by_id(self, tag_id, content):
        """

        Args:
            tag_id:
            content:

        Returns:

        """
        username_input = self.driver.find_element(by=By.ID, value=tag_id)
        SeleniumUtility.wait_element_is_displayed(username_input)
        username_input.send_keys(content)

    def change_to_new_window(self):
        """

        Returns:

        """
        windows = self.driver.window_handles
        current_window = self.driver.current_window_handle
        for window in windows:
            if window != current_window:
                self.driver.switch_to.window(window)

    def switch_to_iframe(self, iframe_element):
        """

        Args:
            iframe_element:

        Returns:

        """
        self.driver.switch_to.frame(iframe_element)

    def switch_to_main_page(self):
        """
        switch to main page.
        Returns:

        """
        self.driver.switch_to.default_content()

    def close(self):
        self.driver.close()
